#![feature(nll)]
extern crate sdl2;
use sdl2::pixels::{Color};
use sdl2::event::{Event, WindowEvent};

pub mod geom;
pub mod frag;
pub mod diff;
use frag::{DrawFrag};

fn main() {
  let sdl = sdl2::init().unwrap();
  let mut events = sdl.event_pump().unwrap();

  let vid = sdl.video().unwrap();
  let mut canvas: sdl2::render::Canvas<_> =
    vid.window("R", 800, 600)
       .build()
       .unwrap()
       .into_canvas()
       .build()
       .unwrap();

  let mut scene =
  {
    use geom::Dim;
    use frag::AlignH::*;
    use frag::AlignV::*;

    frag::pack_lr(vec![
      frag::rect(Dim(40, 40), Color::RGB(255, 0, 0)),
      frag::rect(Dim(40, 40), Color::RGB(0, 180, 220)),
      frag::rect(Dim(40, 40), Color::RGB(255, 255, 0)),
      frag::fill(Color::RGB(10, 90, 20)),
    ])
      .align(Left, Top)
      .spacing(3)
      .container_bg(20, Color::RGB(255, 255, 255))
      .width(600)
      .height(400)
      .align(Center, Middle)
  };

  scene.calculate_geometry(geom::Dim(800, 600));

  'renderloop: loop {
    for evt in events.poll_iter() {
      match evt {
        Event::Quit { .. } =>
          break 'renderloop,

        Event::Window { win_event: WindowEvent::Resized(w, h), .. } => {
          scene.calculate_geometry(geom::Dim(w as u32, h as u32));
        }

        _ => (),
      }
    }

    canvas.set_draw_color(Color::RGB(0, 0, 0));
    canvas.clear();
    canvas.draw_frag(&scene);
    canvas.present();
    std::thread::sleep(
      std::time::Duration::from_micros(1000000 / 64)
    );
  }
}
